// DialogUser.cpp : implementation file
//

#include "stdafx.h"
#include "ECGWorkStation.h"
#include "DialogUser.h"
#include "..\public\BiocareDB\BiocareDB.h"
#include "..\public\pubfun\inireadwrite\inireadwrite.h"
#include "Globals.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CDialogUser dialog


CDialogUser::CDialogUser(CWnd* pParent /*=NULL*/)
	: CDialog(CDialogUser::IDD, pParent)
{
	//{{AFX_DATA_INIT(CDialogUser)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
}


void CDialogUser::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CDialogUser)
	DDX_Control(pDX, IDC_LIST_USER, m_listUser);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CDialogUser, CDialog)
	//{{AFX_MSG_MAP(CDialogUser)
	ON_BN_CLICKED(IDC_BUTTON_ADD, OnButtonAdd)
	ON_BN_CLICKED(IDC_BUTTON_DELETE, OnButtonDelete)
	ON_BN_CLICKED(IDC_BUTTON_UPDATE, OnButtonUpdate)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CDialogUser message handlers

BOOL CDialogUser::OnInitDialog() 
{
	CDialog::OnInitDialog();
	
	InitCtrls();
	if(!BuildListFromDB()) return FALSE;
	
	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}
BOOL CDialogUser::BuildListFromDB(void)
{
	char szErrorMsg[1024];
	memset(szErrorMsg, 0, 1024);
	BCDBDataSets* pDataSets = NULL;
	BCDBCreateDataSets(&pDataSets, szErrorMsg);

	BOOL bRet = FALSE;
	BCDBConnection DBConn;
	if(!BCDBConnectDB(&DBConn, (LPCTSTR)theApp.m_strConnection, (LPCTSTR)theApp.m_strConnUserName, (LPCTSTR)theApp.m_strConnPassWord, szErrorMsg)) return FALSE;
	if(!BCDBQueryDBData(&DBConn, T_USER, NULL, &pDataSets, szErrorMsg)){
		BCDBDisconnectionDB(&DBConn, szErrorMsg);
		BCDBClearDataSets(pDataSets, szErrorMsg);

		return FALSE;
	}
	BCDBDisconnectionDB(&DBConn, szErrorMsg);

	BCDBDataSets::iterator itDataSets = pDataSets->begin();
	BCDBDataElem* pData = NULL;
	CString strUserID, strUserName, strRealName, strUserType, strUserDesc;
	for (; itDataSets != pDataSets->end(); ++itDataSets)
	{
		BCDBDataSet* pDataSetResult = *itDataSets;
		BCDBDataSet::iterator itDataSet;
		for (itDataSet = pDataSetResult->begin(); itDataSet != pDataSetResult->end(); ++itDataSet)
		{
			pData = *itDataSet;
			if (!strncmp(pData->szName, T_USER_ID, strlen(T_USER_ID))) {
				strUserID.Format("%s", (char*)pData->pValue);
			}
			if (!strncmp(pData->szName, T_USER_NAME, strlen(T_USER_NAME))) {
				strUserName.Format("%s", (char*)pData->pValue);
			}
			if (!strncmp(pData->szName, T_USER_TYPE, strlen(T_USER_TYPE))) {
				strUserType.Format("%s", (char*)pData->pValue);
			}
			if (!strncmp(pData->szName, T_USER_REALNAME, strlen(T_USER_REALNAME))) {
				strRealName.Format("%s", (char*)pData->pValue);
			}
			if (!strncmp(pData->szName, T_USER_DESC, strlen(T_USER_DESC))) {
				strUserDesc.Format("%s", (char*)pData->pValue);
			}
			pData = NULL;
		}

		m_listUser.InsertItem(m_listUser.GetItemCount(), strUserName);
		m_listUser.SetItemText(m_listUser.GetItemCount()-1, 1, strRealName);
		m_listUser.SetItemText(m_listUser.GetItemCount()-1, 2, strUserType);
		m_listUser.SetItemText(m_listUser.GetItemCount()-1, 3, strUserDesc);
		m_listUser.SetItemText(m_listUser.GetItemCount()-1, 4, strUserID);
	}

	BCDBClearDataSets(pDataSets, szErrorMsg);

	return TRUE;
}
void CDialogUser::InitCtrls(void)
{
	CString strText = "";
	m_listUser.SetExtendedStyle(LVS_EX_GRIDLINES|LVS_EX_FULLROWSELECT);
	strText.LoadString(IDS_STRING103);
	m_listUser.InsertColumn(0, strText, 0, 50);
	strText.LoadString(IDS_STRING105);
	m_listUser.InsertColumn(1, strText, 0, 80);
	strText.LoadString(IDS_STRING1017);
	m_listUser.InsertColumn(2, strText, 0, 80);
	strText.LoadString(IDS_STRING111);
	m_listUser.InsertColumn(3, strText, 0, 80);
	m_listUser.InsertColumn(4, "ID", 0, 0);
}
void CDialogUser::RefreshList(void)
{
	m_listUser.DeleteAllItems();
	BuildListFromDB();
}

#include "DialogUserEdit.h"
void CDialogUser::OnButtonAdd() 
{
	CDialogUserEdit dlg(NULL, MODE_ADD);
	dlg.DoModal();
	RefreshList();
	
}

void CDialogUser::OnButtonDelete() 
{
	int nSelUser = -1;
	POSITION pos = m_listUser.GetFirstSelectedItemPosition();
	if(pos == NULL){
		CString strErrorMsg = "";
		strErrorMsg.LoadString(IDS_STRING117);
		AfxMessageBox(strErrorMsg);
		return;
	}

	nSelUser = m_listUser.GetNextSelectedItem(pos);

	CString strUserName = "";
	strUserName = m_listUser.GetItemText(nSelUser, 0);
	strUserName.TrimLeft();strUserName.TrimRight();

	CECGWorkStationApp* pApp = (CECGWorkStationApp*)AfxGetApp();
	if (!strUserName.CompareNoCase(pApp->m_strUserName)) {
		CString strErrorMsg = "";
		strErrorMsg.LoadString(IDS_STRING1018);
		AfxMessageBox(strErrorMsg);

		return;
	}
	CString strText = "", strTitle = "";
	strText.LoadString(IDS_STRING1019);
	strTitle.LoadString(IDS_STRING1020);
	if (MessageBox(strText, strTitle, MB_OKCANCEL|MB_ICONERROR) == IDCANCEL) {
		return;
	}

	char szErrorMsg[1024];
	memset(szErrorMsg, 0, 1024);
	CString strID = "";
	strID = m_listUser.GetItemText(nSelUser, 4);
	BOOL bRet = FALSE;
	BCDBConnection DBConn;
	if(!BCDBConnectDB(&DBConn, (LPCTSTR)theApp.m_strConnection, (LPCTSTR)theApp.m_strConnUserName, (LPCTSTR)theApp.m_strConnPassWord, szErrorMsg)) return;
	bRet = BCDBDeleteDBData(&DBConn, T_USER, (LPCTSTR)strID, szErrorMsg);
	bRet = BCDBDisconnectionDB(&DBConn, szErrorMsg);
	m_listUser.DeleteItem(nSelUser);
	
}

void CDialogUser::OnButtonUpdate() 
{
	CDialogUserEdit dlg(NULL, MODE_UPDATE);
	int nSelUser = -1;
	POSITION pos = m_listUser.GetFirstSelectedItemPosition();
	if(pos == NULL){
		CString strErrorMsg = "";
		strErrorMsg.LoadString(IDS_STRING117);
		AfxMessageBox(strErrorMsg);
		return;
	}
	nSelUser = m_listUser.GetNextSelectedItem(pos);

	dlg.m_strUserName = m_listUser.GetItemText(nSelUser, 0);
	dlg.m_strTrueName = m_listUser.GetItemText(nSelUser, 1);
	dlg.m_nUserType = atoi(m_listUser.GetItemText(nSelUser, 2));
	dlg.m_strMemo = m_listUser.GetItemText(nSelUser, 3);
	dlg.m_strID = m_listUser.GetItemText(nSelUser, 4);
	dlg.DoModal();
	RefreshList();
	
}
